<?php

class Registration extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper("url");
        $this->load->library('my_layout');
        $this->my_layout->setLayout("layout/frontend");
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->model('users_model');
        //load center
        $this->load->model('center_model');
    }

    function index() {
        //load center menu
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        
        $this->my_layout->view('frontend/thanhvien/regist_view', $data);
    }

    function test() {
        $data = array();
        $data['error'] = NULL;
        $data['publickey'] = '6Lc9Z-8SAAAAAOa298dF4Lw1EG1-f7xmoB2ij-T6';
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        
        $this->my_layout->view('frontend/thanhvien/regist_view', $data);
    }

    public function submit_registration() {
        if ($this->input->post()) {
            // field name, error message, validation rule
            $this->form_validation->set_rules('username', 'Tài khoản', 'trim|required|min_length[4]|is_unique[users.username]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
            $this->form_validation->set_rules('con_password', 'Xác nhận password', 'trim|required|matches[password]');
            $this->form_validation->set_rules('email_address', 'Email', 'trim|required|valid_email|is_unique[users.email]');
            $this->form_validation->set_rules('con_email', 'Xác nhận email', 'trim|required|matches[email_address]');
            $this->form_validation->set_rules('sexual', 'gioitinh', 'trim|required|xss_clean');

            if ($this->form_validation->run() == FALSE) {

                $this->test();
            } else {
                $this->users_model->add_user();
                //TO DO: gui email kich hoat
                $data['username'] = $this->input->post('username');
                $password = $this->input->post('password');
                $data['password'] = '***'.substr($password, -3);
                $data['email'] = $this->input->post('email_address');
                
                
                 $data['domain'] = base_url().'index.php/website/home';
                $data['active_link'] = base_url() . 'index.php/website/thanhvien/registration/activeUser/' . $this->input->post('username');
               
                $this->load->library('send_email_c');
                $email =  new $this->send_email_c();
                
                $email->send_active($data);
                
                $data['typeCenters'] = $this->center_model->get_type();
                $data['idType'] = NULL;
                
                $this->my_layout->view('frontend/thanhvien/thank_for_regist', $data);
            }
        }
    }
    
    function activeUser($id) {
        $data['msg']= $this->users_model->active_user($id);
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        if($data['msg'] == 0)
            redirect('website/thanhvien/registration/load_login');
        else
            $this->my_layout->view('frontend/thanhvien/active_view', $data);    
    }

    public function no_login() {
        $this->load->helper(array('form'));
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
        if ($this->form_validation->run($this) == FALSE) {
            $this->my_layout->view2('user/thanhvien/signin_view', 'user/thanhvien/no_signin', $data = '');
        } else {
            $this->home();
        }
    }
    
    public function load_login(){
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        $this->my_layout->view('frontend/thanhvien/signin_view', $data);
    }

    public function login() {
        
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        
        $this->load->helper(array('form'));
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
        $path = '';
        if (!$this->check_database($this->input->post('password'))) {
            $path = 'frontend/thanhvien/popup_notlogin';
            //$path = 'frontend/thanhvien/popup_suggest_reg';
        }
        elseif (!$this->check_active($this->input->post('password'))) {
             $path = 'frontend/thanhvien/popup_active';
         } 
        else {
            $path = 'frontend/thanhvien/popup_suggest_reg';
            //$path = 'frontend/thanhvien/popup_notlogin';
           
        }

        if ($path == 'frontend/thanhvien/popup_suggest_reg') {

            $this->home();
        } else {
            $this->my_layout->view2('frontend/thanhvien/signin_view', $path, $data);
            
        }

    }


     public function check_active($password) {
        $username = $this->input->post('username');
        $result = $this->users_model->login_active($username, $password);
        if ($result)
        { 
            $this->form_validation->set_message('check_database', 'Tài khoản hoặc mật khẩu không đúng');
            return false;
        }
        return true;
    }

    public function check_database($password) {
        $username = $this->input->post('username');
        $result = $this->users_model->login($username, $password);
        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                $sess_array = array(
                    'id' => $row->id_user,
                    'sign_in' => TRUE,
                );
                $this->session->set_userdata('logged_in', $sess_array);
            }
            return TRUE;
        } else {
            $this->form_validation->set_message('check_database', 'Tài khoản hoặc mật khẩu không đúng');
            return false;
        }
    }
    
    function logout() {
        $this->session->unset_userdata('logged_in');
        redirect('website/home', 'refresh');
        session_destroy();
    }

    function home() {
        $data['typeCenters'] = $this->center_model->get_type();
        $data['idType'] = NULL;
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            //$this->load_html('frontend/home_view', $data);
            redirect('website/home');
        } else {
            redirect('website/thanhvien/registration/login', 'refresh');
        }
    }

}
